package controllers.manage.fitness;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import models.venue.enums.PolicyType;
import models.venue.fitness.Fitnesses;
import models.venue.policy.Policy;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by buhaoba on 2017/4/11.
 */
@With(BaseController.class)
public class PolicyCtrl extends Controller {
    /**
     * 跳转到健身类型管理页面
     */
    public static void index(Integer pageNumber, Long fitnessId){

        Merchant merchant = Secure.getLoginUser().merchant;

        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String , Object> searchMap = new HashMap<>();

        if(fitnessId != null && fitnessId > 0) {
            searchMap.put("fitnessId", fitnessId);
            renderArgs.put("fitnessId" , fitnessId);
        }

        if(merchant != null){
            searchMap.put("merchantId", merchant.id);
        }

        searchMap.put("policyType", PolicyType.FITNESS);

        List<Fitnesses> fitnesses = Fitnesses.findAllFitnessesByMerchant(merchant.id);

        flash.put("menuId", "10");

        JPAExtPaginator<Policy> resultPage = Policy.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber , fitnesses , fitnessId);
    }

    /**
     * 跳转到新增健身类型页面
     */
    public static void add(){
        Merchant merchant = Secure.getLoginUser().merchant;
        List<Fitnesses> fitnesses = Fitnesses.findAllFitnessesByMerchant(merchant.id);
        render(fitnesses);
    }

    /**
     * 跳转到修改健身类型页面
     */
    public static void modify(Long id, Integer pageNumber){
        Merchant merchant = Secure.getLoginUser().merchant;
        List<Fitnesses> fitnesses = Fitnesses.findAllFitnessesByMerchant(merchant.id);
        Policy policy = Policy.findById(id);
        render(policy, pageNumber , fitnesses);
    }


    /**
     * 创建健身类型
     */
    public static void create(Policy policy){

        Merchant merchant = Secure.getLoginUser().merchant;
        policy.merchant = merchant;

        policy.deleted = DeletedStatus.UN_DELETED;
        String startTime = policy.startTime;
        if(startTime.split(":")[0].length() == 1){
            policy.startTime = "0"+startTime;
        }
        String endTime = policy.endTime;
        if(endTime.split(":")[0].length() == 1){
            policy.endTime = "0"+endTime;
        }

        policy.policyType = PolicyType.FITNESS;
        policy.save();
        flash.put("msg" , "健身类型设置价格成功");
        redirect("/BS0027/admin/policy/index/1");
    }

    /**
     * 修改健身类型
     */
    public static void update(Long id, Policy policy, Integer pageNumber){
        String startTime = policy.startTime;
        if(startTime.split(":")[0].length() == 1){
            policy.startTime = "0"+startTime;
        }
        String endTime = policy.endTime;
        if(endTime.split(":")[0].length() == 1){
            policy.endTime = "0"+endTime;
        }

        Policy.update(id , policy);
        flash.put("msg" , "健身类型价格价格成功");
        redirect("/BS0027/admin/policy/index/"+pageNumber);
    }

    /**
     * 删除健身类型
     */
    public static void delete(Long id , Integer pageNumber){
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Policy policy = Policy.findById(id);
        policy.deleted=DeletedStatus.DELETED;
        policy.save();
        flash.put("msg" , "健身类型价格删除成功");
        redirect("/BS0027/admin/policy/index/"+pageNumber);
    }
}
